package com.mvc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mvc.dao.GenericDaoImpl;
import com.mvc.entity.Icon_User;

@Service
public class UserService {
  
  @Autowired
  public GenericDaoImpl entityDao;

  public List<Icon_User> findAll() {
    List<Icon_User> list = entityDao.loadAll(Icon_User.class);
    return list;
  }

  public Icon_User find(Long id) {
    return (Icon_User) entityDao.load(Icon_User.class, id);
  }
  
  public String findSessionIdByUsername(String username){
    Map<String, String> param = new HashMap<String, String>();
    param.put("username", username);
    List list = entityDao.find("select sessionId from Icon_User where username=:username",param);
    String sessionId = null;
    for (int i = 0; i < list.size(); i++) {
      sessionId = (String) list.get(i);
    }
    return sessionId;
  }
  
  public Icon_User findUserByUsername(String username){
    Map<String, String> param = new HashMap<String, String>();
    param.put("username", username);
    List list = entityDao.find("select id from Icon_User where username=:username",param);
    Icon_User user = null;
    Long id=0L;
    if(list.size()==1){
    for (int i = 0; i < list.size(); i++) {
      id = Long.parseLong(list.get(i).toString());
    }
      return (Icon_User) entityDao.findById(Icon_User.class, id);
    }else{
      return null;
    }
  }

  public void save(Icon_User user) {
    entityDao.save(user);
  }

  public void delete(Icon_User user) {
    entityDao.delete(user);
  }
}
